package com.jianli.smart.core;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import com.jianli.smart.core.annotation.Controller;
import com.jianli.smart.core.annotation.Service;
import com.jianli.smart.core.utils.ClassUtil;
import com.jianli.smart.core.utils.ConfigReader;

public final class ClassHelper {
	private static Set<Class<?>> CLASS_SET;
	
	static{
		CLASS_SET = ClassUtil.getClassSet(ConfigReader.getBasePath());
	}
	
	public static Set<Class<?>> getClassSet(){
		return CLASS_SET;
	}
	
	public static Set<Class<?>> getSet(
	        Class<? extends Annotation> annotationClass){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		for(Class<?> cls:CLASS_SET){
			if(cls.isAnnotationPresent(annotationClass)){
				classSet.add(cls);
			}
		}
		return classSet;
	}
	
	public static Set<Class<?>> getBeanSet(){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		classSet.addAll(getSet(Service.class));
		classSet.addAll(getSet(Controller.class));
		return classSet;
	}
	
	/**
	 * 获取带有某类（接口）下的所有子类（或实现类）
	 * @param superClass
	 * @return
	 */
	public static Set<Class<?>> getClassSetBySuper(Class<?> superClass){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		for(Class<?> cls:CLASS_SET){
			if(superClass.isAssignableFrom(cls) && !superClass.equals(cls)){
				classSet.add(cls);
			}
		}
		return classSet;
	}
}
